//
//  groundTagsView.m
//  SoundPill
//
//  Created by 宋鹏鹏 on 15/6/10.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "groundTagsView.h"
@interface groundTagsView ()
{
    float x;
    BOOL isFirst;
}
@end
@implementation groundTagsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        isFirst = YES;
        x = 0;
    }
    return self;
}
- (void)createBackgroundView
{
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *visual = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    visual.frame = self.bounds;
    [self addSubview:visual];
}
- (void)createTagView:(NSArray *)tags
{
    //x = 10;
    if (tags.count > 3) {
        for (int i = 2; i >= 0; i--) {
            NSString *tag = [NSString stringWithFormat:@"，%@",tags[i]];
            if (i == 0) {
                tag = tags[i];
            }
            self.tagLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            _tagLabel.userInteractionEnabled = YES;
            _tagLabel.font = [UIFont boldSystemFontOfSize:12];
            [_tagLabel setNumberOfLines:0];
            _tagLabel.layer.cornerRadius = 1;
            [_tagLabel.layer setMasksToBounds:YES];
//            _tagLabel.layer.borderColor = [[UIColor blackColor] CGColor];
//            _tagLabel.layer.borderWidth = 1.3;
            _tagLabel.textAlignment = NSTextAlignmentCenter;
            _tagLabel.textColor = [UIColor blackColor];
            CGFloat w = [self setLabelText:tag];
            _tagLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 20 - x , 4, w, 17);
            _tagLabel.backgroundColor = [UIColor whiteColor];
            [self addSubview:_tagLabel];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTagTouch:)];
            [_tagLabel addGestureRecognizer:tap];
        }

    }else{
    for (int i = (int)tags.count - 1; i >= 0; i--) {
        NSString *tag = [NSString stringWithFormat:@"，%@",tags[i]];
        if (tags.count == 1 || i == 0) {
            tag = tags[i];
        }
        self.tagLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _tagLabel.userInteractionEnabled = YES;
                    _tagLabel.font = [UIFont boldSystemFontOfSize:10];
        [_tagLabel setNumberOfLines:0];
        _tagLabel.layer.cornerRadius = 1;
        [_tagLabel.layer setMasksToBounds:YES];
//        _tagLabel.layer.borderColor = [[UIColor blackColor] CGColor];
//        _tagLabel.layer.borderWidth = 1.5;
        _tagLabel.textAlignment = NSTextAlignmentCenter;
        _tagLabel.textColor = [UIColor blackColor];
        CGFloat ww = [self setLabelText:tag];
        _tagLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 20 - x , 4, ww, 17);
        _tagLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:_tagLabel];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTagTouch:)];
        [_tagLabel addGestureRecognizer:tap];

    }
    }
    self.tagImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plaza_tag"]];
    _tagImage.frame = CGRectMake(_tagLabel.frame.origin.x - 20, 0, 22, 22);
    [self addSubview:_tagImage];
    _tagImage.center = CGPointMake(_tagImage.center.x, _tagLabel.center.y);
}
- (CGFloat)setLabelText:(NSString *)text{
    NSString *firstStr = [text substringToIndex:1];
    NSString *str = @"";
    if ([firstStr isEqualToString:@"，"]) {
        text = [text substringFromIndex:1];
        str = [self checkTextIsEquelClassify:text];
        str = [NSString stringWithFormat:@"，%@",str];
    } else {
        str = [self checkTextIsEquelClassify:text];
    }
    UIFont *font = [UIFont fontWithName:@"Arial" size:12];
    _tagLabel.font = font;
    _tagLabel.text = str;
    CGSize deSize = [_tagLabel sizeThatFits:CGSizeMake(90,30)];
    CGFloat Width = deSize.width;

        x = x + Width;
//    }

    return Width;
}
- (NSString *)checkTextIsEquelClassify:(NSString *)text
{
    if ([text isEqualToString:@"记#录"]) {
        return @"记录";
    } else if ([text isEqualToString:@"点#评"]) {
        return @"点评";
    } else if ([text isEqualToString:@"谈#话"]) {
        return @"谈话";
    } else if ([text isEqualToString:@"音#乐"]) {
        return @"音乐";
    } else if ([text isEqualToString:@"买#卖"]) {
        return @"买卖";
    } else if ([text isEqualToString:@"读#书"]) {
        return @"读书";
    } else if ([text isEqualToString:@"话#题"]) {
        return @"话题";
    } else if ([text isEqualToString:@"观#点"]) {
        return @"观点";
    } else if ([text isEqualToString:@"段#子"]) {
        return @"段子";
    } else if ([text isEqualToString:@"其#他"]) {
        return @"其他";
    } else if ([text isEqualToString:@"故#事"]) {
        return @"故事";
    } else if ([text isEqualToString:@"教#学"]) {
        return @"教学";
    } else if ([text isEqualToString:@"旅#行"]) {
        return @"旅行";
    } else if ([text isEqualToString:@"影#评"]) {
        return @"电影";
    } else if ([text isEqualToString:@"电#影"]) {
        return @"电影";
    }
    else {
        return text;
    }
}
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(200, 40)//限制最大的宽度和高度
                                       options: NSStringDrawingUsesFontLeading//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    
    return rect.size;
}
- (void)labelTagTouch:(UITapGestureRecognizer *)rec
{
    UILabel *label = (UILabel *)rec.view;
    NSString *labelText;
    if ([[label.text substringToIndex:1] isEqualToString:@"，"]) {
        labelText = [label.text substringFromIndex:1];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"searchText" object:labelText];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"searchText" object:label.text];
    }
    
}
 
@end
